/*
 * Copyright ©2015-2021 Jaemon. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.jaemon.spring.adorner.type;

import io.gitee.jaemon.spring.adorner.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Doc读取器对象
 *
 * @author Jaemon
 * @since 1.0
 */
public class DocScanner {
    private static final String DEFAULT_CLASSPATH = System.getProperty("user.dir") + File.separator + "src\\main\\java";
    /** 待扫描的路径 */
    private List<String> packages;
    /** 源码文件路径, 默认(user.dir/src/main/java) */
    private String classPath;

    private DocScanner(List<String> packages, String classPath) {
        this.packages = packages;
        this.classPath = classPath;
    }

    /**
     * 实例化读取器对象
     *
     * @param packageName
     *      待扫描的路径
     * @return
     *      docScanner {@link DocScanner}
     */
    public static DocScanner of(String packageName) {
        List<String> packages = new ArrayList<>();
        packages.add(packageName);
        return new DocScanner(packages, DEFAULT_CLASSPATH);
    }

    /**
     * 实例化读取器对象
     *
     * @param packageName
     *      待扫描的路径
     * @param classpath
     *      源码文件路径
     * @return
     *      docScanner {@link DocScanner}
     */
    public static DocScanner of(String packageName, String classpath) {
        List<String> packages = new ArrayList<>();
        packages.add(packageName);
        return new DocScanner(packages, classpath);
    }

    /**
     * 实例化读取器对象
     *
     * @param packages
     *      待扫描注释的包路径集
     * @return
     *      docScanner {@link DocScanner}
     */
    public static DocScanner of(List<String> packages) {
        return new DocScanner(packages, DEFAULT_CLASSPATH);
    }

    /**
     * 实例化读取器对象
     *
     * @param packages
     *      待扫描的路径
     * @param classPath
     *      源码文件路径
     * @return
     *      docScanner {@link DocScanner}
     */
    public static DocScanner of(List<String> packages, String classPath) {
        if (StringUtils.isEmpty(classPath)) {
            of(packages);
        }
        return new DocScanner(packages, classPath);
    }

    public List<String> getPackages() {
        return packages == null ? new ArrayList<>() : packages;
    }

    public String getClassPath() {
        return classPath;
    }

    @Override
    public String toString() {
        return String.format("package=%s, classpath=[%s]", packages, classPath);
    }
}